package com.wyh;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashSet;
import java.util.Set;

/**
 * @author wuyuhan
 * @date 2021/3/27 22:57
 */
@SpringBootApplication
public class RedisClusterApplication {
    public static void main(String[] args) {
        SpringApplication.run(RedisClusterApplication.class, args);
        // 添加集群的服务节点Set集合
        Set<HostAndPort> hostAndPortsSet = new HashSet<HostAndPort>();
        // 添加节点
        hostAndPortsSet.add(new HostAndPort("192.168.44.3", 7001));
        hostAndPortsSet.add(new HostAndPort("192.168.44.3", 7002));
        hostAndPortsSet.add(new HostAndPort("192.168.44.3", 7003));
        hostAndPortsSet.add(new HostAndPort("192.168.44.3", 7004));
        hostAndPortsSet.add(new HostAndPort("192.168.44.3", 7005));
        hostAndPortsSet.add(new HostAndPort("192.168.44.3", 7006));
        hostAndPortsSet.add(new HostAndPort("192.168.44.3", 7007));
        hostAndPortsSet.add(new HostAndPort("192.168.44.3", 7008));
        // Jedis连接池配置
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        // 最大空闲连接数, 默认8个
        jedisPoolConfig.setMaxIdle(100);
        // 最大连接数, 默认8个
        jedisPoolConfig.setMaxTotal(500);
        //最小空闲连接数, 默认0
        jedisPoolConfig.setMinIdle(0);
        // 获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间,  默认-1
        jedisPoolConfig.setMaxWaitMillis(2000); // 设置2秒
        //对拿到的connection进行validateObject校验
        jedisPoolConfig.setTestOnBorrow(true);
        JedisCluster jedis = new JedisCluster(hostAndPortsSet, jedisPoolConfig);
        System.out.println("增加数据：" + jedis.set("name:2", "liubei"));
        System.out.println("获取key值为name:2对应的value数据：" + jedis.get("name:2"));
    }
}
